﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

using System.Windows.Controls;
using Ricciolo.Controls;
using OpenExpressApp.WPF.Controls;
using OpenExpressApp.Module.WPF.Editors.TreeColumn;

namespace OpenExpressApp.Module.WPF.Command.WpfReport
{
    internal class LoadReportData
    {
        /// <summary>
        /// 显示层级关系
        /// </summary>
        private string _level = "-";

        private string _tableName;

        public LoadReportData(string tableName)
        {
            this._tableName = tableName;
        }
        public LoadReportData(ListObjectView view)
        {
            this._tableName = view.BOType.Name;
        }

        /// <summary>
        /// 给DataTable装载数据
        /// </summary>
        /// <param name="control"></param>
        /// <returns></returns>
        internal DataTable LoadData(object control)
        {
            if (control is DataGrid)
            {
                return LoadFromGrid(control as DataGrid);
            }
            else if (control is MultiObjectTreeView)
            {
                TreeListView tree = (control as MultiObjectTreeView).Tree;
                return LoadFromTree(tree);
            }
            else
            {
                return LoadFromTree(control as TreeListView);
            }
        }

        /// <summary>
        /// 从DataGrid装载
        /// </summary>
        /// <param name="grid"></param>
        /// <returns></returns>
        private DataTable LoadFromGrid(DataGrid grid)
        {
            DataTable table = new DataTable(this._tableName);
            //加载表头
            foreach (var col in grid.Columns)
            {
                table.Columns.Add(col.Header.ToString());
            }
            //加载数据
            for (int i = 0; i < grid.Items.Count; i++)
            {
                object[] obj = new object[grid.Columns.Count];
                for (int j = 0; j < grid.Columns.Count; j++)
                {
                    DataGridCell cell = WpfCellHelper.GetCell(grid, i, j);
                    if (cell.Content is TextBlock)
                    {
                        obj[j] = ((TextBlock)cell.Content).Text;
                    }
                    if (cell.Content is CheckBox)
                    {
                        obj[j] = (bool)((CheckBox)cell.Content).IsChecked ? "是" : "否";
                    }
                }
                table.Rows.Add(obj);
            }
            return table;
        }

        /// <summary>
        /// 从树中装载数据
        /// </summary>
        /// <param name="tree"></param>
        /// <returns></returns>
        private DataTable LoadFromTree(TreeListView tree)
        {
            if (tree == null)
                throw new ArgumentNullException("need TreeListView,arg is null");
            DataTable table = new DataTable(this._tableName);
            //加载表头
            foreach (var col in tree.Columns)
            {
                table.Columns.Add(col.Header.ToString());
            }
            //加载数据
            for (int i = 0; i < tree.Items.Count; i++)
            {
                object cur = (tree.Items[i] as TreeListViewItem).Header;
                object[] obj = new object[tree.Columns.Count];
                for (int j = 0; j < tree.Columns.Count; j++)
                {
                    string propertyName = (string)tree.Columns[j].GetValue(Control.NameProperty);
                    BusinessObjectPropertyInfo property = (BusinessObjectPropertyInfo)tree.Columns[j].GetValue(WPFMeta.BOPropInfoProperty);
                    obj[j] = this.GetCurValue(cur, property, propertyName);
                }
                table.Rows.Add(obj);
                this.GetRow(tree.Items[i] as TreeListViewItem, table, tree);
            }
            return table;
        }

        /// <summary>
        /// 找子项
        /// </summary>
        /// <param name="item"></param>
        /// <param name="table"></param>
        /// <param name="tree"></param>
        private void GetRow(TreeListViewItem item, DataTable table, TreeListView tree)
        {
            _level += "-";
            for (int i = 0; i < item.Items.Count; i++)
            {
                TreeListViewItem childItem = item.Items[i] as TreeListViewItem;
                if (childItem != null)
                {

                    object cur = childItem.Header;
                    object[] obj = new object[tree.Columns.Count];
                    for (int j = 0; j < tree.Columns.Count; j++)
                    {
                        string propertyName = (string)tree.Columns[j].GetValue(Control.NameProperty);
                        BusinessObjectPropertyInfo property = (BusinessObjectPropertyInfo)tree.Columns[j].GetValue(WPFMeta.BOPropInfoProperty);
                        if (j == 0)
                        {
                            obj[j] = _level + this.GetCurValue(cur, property, propertyName);
                        }
                        else
                        {
                            obj[j] = this.GetCurValue(cur, property, propertyName);
                        }
                    }
                    table.Rows.Add(obj);
                    this.GetRow(childItem, table, tree);
                }
            }
            _level = _level.Substring(_level.IndexOf("-") + 1);
        }

        /// <summary>
        /// 获取当前属性的值
        /// </summary>
        /// <returns></returns>
        private string GetCurValue(object cur, BusinessObjectPropertyInfo property, string propertyName)
        {
            try
            {
                if (null != cur.GetType().GetProperty(propertyName) && null != cur.GetPropertyValue(propertyName))
                {
                    if (property.IsLookup)
                    {
                        string lookupName = property.LookupAttribute.LookupPropertyName;
                        object model = cur.GetPropertyValue(lookupName);
                        IEntityInfo lookupModelInfo = ApplicationModel.GetBusinessObjectInfo(model.GetType());
                        var lookupModelTitleProperty = lookupModelInfo.BOPropertyInfos.First(p => p.ViewInfo().IsTitle);
                        return model.GetPropertyValue(lookupModelTitleProperty.Name).ToString();
                    }
                    else
                    {
                        return cur.GetPropertyValue(propertyName).ToString();
                    }
                }
                else
                {
                    return string.Empty;
                }
            }
            catch
            {
                return string.Empty;
            }
        }
    }
}
